package com.edfu.core.contentmgt.jcr.service.impl;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import com.edfu.core.contentmgt.jcr.util.ContentMgtUtil;

/**
 * @author Sahil Sharma
 */
public class JCRFactoryUtil {

	private static IJCRFactory _jcrFactory;

	public static IJCRFactory getJCRFactory() {
		if (_jcrFactory == null) {
			_jcrFactory = (IJCRFactory) JCRBeanLocatorUtil
					.locateBean("com.edfu.core.contentmgt.jcr.service.impl.jackrabbit.JCRFactoryImpl");
		}
		return _jcrFactory;
	}
	
	//Create Admin Session.
	//All the Content Mgt Operations pre 1.6 are performed by the Admin and this api is used to created the session.
	public static Session createSession(String workspaceName, ContentServiceAccessType accessType)
		throws RepositoryException {
		if ( workspaceName == null ) {
			throw new RuntimeException("Workspace Name cannot be null");
		}
		if ( accessType.name().equals(ContentServiceAccessType.WEBDAV.name())) {
			return createWebDavSession(workspaceName);
		}else if ( accessType.name().equals(ContentServiceAccessType.RMI.name())){
			return createRMISession(workspaceName);
		}else if ( accessType.name().equals(ContentServiceAccessType.TRANSIENT.name())){
			return createTransientSession(workspaceName);
		}else {
			throw new RuntimeException("Unsupported access type");
		}
	}
	
	//Create Session using the supplied credentials
	public static Session createSession(String userName, String password, String workspaceName, ContentServiceAccessType accessType)
		throws RepositoryException {
		//Create an admin session if no username and password is supplied.Need to revisit this
		if ( ContentMgtUtil.isNullOrEmpty(userName) && ContentMgtUtil.isNullOrEmpty(password))
			return createSession(workspaceName, accessType);
		
		if ( workspaceName == null ) {
			throw new RuntimeException("Workspace Name cannot be null");
		}
		if ( accessType.name().equals(ContentServiceAccessType.WEBDAV.name())) {
			return createWebDavSession(userName, password, workspaceName);
		}else if ( accessType.name().equals(ContentServiceAccessType.RMI.name())){
			return createRMISession(userName, password, workspaceName);
		}else if ( accessType.name().equals(ContentServiceAccessType.TRANSIENT.name())){
			return createTransientSession(userName, password, workspaceName);
		}else {
			throw new RuntimeException("Unsupported access type");
		}
	}
	
	//Creating an Admin WebDav Session
	private static Session createWebDavSession(String workspaceName)
			throws RepositoryException {
		return getJCRFactory().createWebDavSession(workspaceName);
	}
	
	//Creating an Admin RMI Session
	private static Session createRMISession(String workspaceName)
			throws RepositoryException {
		return getJCRFactory().createRMISession(workspaceName);
	}
	
	//Creating an Admin Transient Session
	private static Session createTransientSession(String workspaceName)
			throws RepositoryException {
		return getJCRFactory().createTransientSession(workspaceName);
	}
	
	//Creating a WebDav Session using supplied credentials
	private static Session createWebDavSession(String userName, String password, String workspaceName)
			throws RepositoryException {
		return getJCRFactory().createWebDavSession(userName, password, workspaceName);
	}
	
	//Creating a RMI Session  using supplied credentials
	private static Session createRMISession(String userName, String password, String workspaceName)
			throws RepositoryException {
		return getJCRFactory().createRMISession(userName, password, workspaceName);
	}
	
	//Creating  a Transient Session using supplied credentials
	private static Session createTransientSession(String userName, String password, String workspaceName)
			throws RepositoryException {
		return getJCRFactory().createTransientSession(userName, password, workspaceName);
	}
	
	
	
	
}
